const app = getApp()
// pages/launch/launch.js
const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];

//获取年
for (let i = 2018; i <= date.getFullYear() + 5; i++) {
  years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  months.push("" + i);
}
//获取日期
for (let i = 1; i <= 31; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  days.push("" + i);
}
//获取小时
for (let i = 0; i < 24; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  hours.push("" + i);
}
//获取分钟
for (let i = 0; i < 60; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  minutes.push("" + i);
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: '',
    multiArray: [years, months, days, hours, minutes],
    multiIndex: [0, 0, 0, 0, 0],
    choose_year: '',


    lottery:'/images/launch_default.jpg',

    index: 0,
    array: ['按时间自动抽奖', '按人数自动抽奖'],

    condition:0,
    number:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //设置默认的年份
    this.setData({
      choose_year: this.data.multiArray[0][0]
    })
    
  },

  // 更换奖品缩略图
  changeLotteryImg:function(){
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original','compressed'],
      sourceType: ['album','camera'],
      success: (res)=>{
        console.log(res.tempFilePaths);
        
        that.setData({
          lottery:res.tempFilePaths
        })
      },
      fail: ()=>{
        wx.showToast({
          title: '图片更换失败',
          icon: 'none',
          duration: 1500,
        });        
      }
    });
  },

  // 提交表单
  formSubmit: function (e) {
    var timestamp2 = Date.parse(new Date(this.data.time));
    timestamp2 = timestamp2 / 1000;

    if(this.data.condition == 0){
      e.detail.value.picker = timestamp2
    }
    console.log(timestamp2);
    
    var userid = app.globalData.acc.id
    var name = e.detail.value.name
    var image = e.detail.value.image
    var condition = this.data.condition
    var picker = e.detail.value.picker
    var detail = e.detail.value.detail
    if(userid && name && image && picker && detail){
      wx.request({
        url: app.globalData.url + 'lotterys/set_lottery',
        data: {
          owner:userid,
          title:name,
          cover:image,
          type:condition,
          condition:picker,
          detail:detail
        },
        method: 'POST',
        success: (res)=>{
          console.log(res);
          
          if(res.data.msg == 1){
            
            var lotteryId = res.data.data
            
            wx.showToast({
              title: '发起成功',
              icon: 'success',
              duration: 1500
            })
            wx.redirectTo({
              url: '/pages/detail/detail?id=' + lotteryId,
            });
          }
          
        },
        fail: ()=>{}
      });
    }else{
      wx.showToast({
        title: '不能为空',
        icon: 'false',
        duration: 2000
      })      
    }
  },


  bindPickerChange: function (e) {
    this.setData({
      condition: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },


  //获取时间日期
  bindMultiPickerChange: function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
    const index = this.data.multiIndex;
    const year = this.data.multiArray[0][index[0]];
    const month = this.data.multiArray[1][index[1]];
    const day = this.data.multiArray[2][index[2]];
    const hour = this.data.multiArray[3][index[3]];
    const minute = this.data.multiArray[4][index[4]];
    this.setData({
      time: year + '-' + month + '-' + day + ' ' + hour + ':' + minute,
    })
  },
  //监听picker的滚动事件
  bindMultiPickerColumnChange: function(e) {
    //获取年份
    if (e.detail.column == 0) {
      let choose_year = this.data.multiArray[e.detail.column][e.detail.value];
      console.log(choose_year);
      this.setData({
        choose_year
      })
    }
    //console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    if (e.detail.column == 1) {
      let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
      let temp = [];
      if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //判断31天的月份
        for (let i = 1; i <= 31; i++) {
          if (i < 10) {
            i = "0" + i;
          }
          temp.push("" + i);
        }
        this.setData({
          ['multiArray[2]']: temp
        });
      } else if (num == 4 || num == 6 || num == 9 || num == 11) { //判断30天的月份
        for (let i = 1; i <= 30; i++) {
          if (i < 10) {
            i = "0" + i;
          }
          temp.push("" + i);
        }
        this.setData({
          ['multiArray[2]']: temp
        });
      } else if (num == 2) { //判断2月份天数
        let year = parseInt(this.data.choose_year);
        console.log(year);
        if (((year % 400 == 0) || (year % 100 != 0)) && (year % 4 == 0)) {
          for (let i = 1; i <= 29; i++) {
            if (i < 10) {
              i = "0" + i;
            }
            temp.push("" + i);
          }
          this.setData({
            ['multiArray[2]']: temp
          });
        } else {
          for (let i = 1; i <= 28; i++) {
            if (i < 10) {
              i = "0" + i;
            }
            temp.push("" + i);
          }
          this.setData({
            ['multiArray[2]']: temp
          });
        }
      }
      console.log(this.data.multiArray[2]);
    }
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    this.setData(data);
  },

})